package com.kiloai.base.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.kiloai.base.AppContext;
import com.kiloai.base.ConstantField;


/**
 * Created by david on 2018/3/6.
 */

public class SharedPreferenceHelper {
    private final String TAG = "SharedPreferenceHelper";
    private static SharedPreferenceHelper helper;
    private static SharedPreferences preferences;


    public static SharedPreferenceHelper getHelper(AppContext context) {
        if (helper == null) {
            synchronized (SharedPreferenceHelper.class) {
                if (helper == null) {
                    helper = new SharedPreferenceHelper();
                }
            }
        }
        if (preferences == null) {
            preferences = context.getSharedPreferences(ConstantField.SHAREDPREFERENCE_NAME, Context.MODE_PRIVATE);
        }
        return helper;
    }

    public SharedPreferenceHelper setStringValue(String key,String value){
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key,value);
        editor.commit();
        LogUtil.e(TAG ,"key=>" + key +",value==>" + value);
        return this;
    }

    public String getStringByKey(String key) {
        if (preferences == null) {
            return "";
        }
        return preferences.getString(key, "");
    }

    public SharedPreferenceHelper setIntValue(String key,int value){
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key,value);
        editor.commit();
        LogUtil.e(TAG ,"key=>" + key +",value==>" + value);
        return this;
    }

    public int getIntByKey(String key) {
        if (preferences == null) {
            return 0;
        }
        return preferences.getInt(key, 0);
    }

    public SharedPreferenceHelper setBooleanValue(String key,boolean value){
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key,value);
        editor.commit();
        LogUtil.e(TAG ,"key=>" + key +",value==>" + value);
        return this;
    }

    public boolean getBooleanByKey(String key) {
        if (preferences == null) {
            return false;
        }
        return preferences.getBoolean(key, false);
    }

    public SharedPreferenceHelper setLongValue(String key,long value){
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(key,value);
        editor.commit();
        LogUtil.e(TAG ,"key=>" + key +",value==>" + value);
        return this;
    }

    public long getLongByKey(String key) {
        if (preferences == null) {
            return 0;
        }
        return preferences.getLong(key,0);
    }
}
